const mutations = {
    ADD_USER(state, user) {
        state.users.push(user);
    },
    DELETE_USER(state, userId) {
        state.users = state.users.filter(user => user.id !== userId);
    },
    UPDATE_USER(state, {index, user}) {
        state.users.splice(index, 1, user);
    },
    ADD_POST(state, post) {
        state.posts.push(post);
    },
    DELETE_POST(state, postId) {
        state.posts = state.posts.filter(post => post.id !== postId);
    },
    APPROVE_POST(state, {index, post}) {
        state.posts.splice(index, 1, post);
        state.approvedPosts.push(post);
    },
    REJECT_POST(state, {index, post}) {
        state.posts.splice(index, 1, post);
    },
    SET_POSTS(state, posts) {
        state.posts = posts;
    },
    SET_USERS(state, users) {
        state.admin.users = users;
    },
    SET_POSTS(state, posts) {
        state.admin.posts = posts;
    }
}

const state = {
    users: [],
    posts: [],
    approvedPosts: []
}
const actions = {
    async createUser({commit}, user) {
        try {
            const response = await axios.post('/api/user', user);
            console.log(response.data); // 打印响应数据
            commit('ADD_USER', response.data); // 提交mutation更新state
        } catch (error) {
            console.error(error);
        }
    },
    async deleteUser({commit}, userId) {
        try {
            const response = await axios.delete(`/api/user/${userId}`);
            console.log(response.data); // 打印响应数据
            commit('DELETE_USER', userId); // 提交mutation更新state
        } catch (error) {
            console.error(error);
        }
    },
    async editUser({commit}, {index, user}) {
        try {
            const response = await axios.put(`/api/user/${user.id}`, user);
            console.log(response.data); // 打印响应数据
            commit('UPDATE_USER', {index, user: response.data}); // 提交mutation更新state
        } catch (error) {
            console.error(error);
        }
    },
    async createPost({commit}, post) {
        try {
            const response = await axios.post('/api/post', post);
            console.log(response.data); // 打印响应数据
            commit('ADD_POST', response.data); // 提交mutation更新state
        } catch (error) {
            console.error(error);
        }
    },
    async deletePost({commit}, postId) {
        try {
            const response = await axios.delete(`/api/post/${postId}`);
            console.log(response.data); // 打印响应数据
            commit('DELETE_POST', postId); // 提交mutation更新state
        } catch (error) {
            console.error(error);
        }
    },
    async approvePost({commit}, {index, post}) {
        try {
            const response = await axios.put(`/api/post/${post.id}/approve`);
            console.log(response.data); // 打印响应数据
            commit('APPROVE_POST', {index, post: response.data}); // 提交mutation更新state
        } catch (error) {
            console.error(error);
        }
    },
    async rejectPost({commit}, {index, post}) {
        try {
            const response = await axios.put(`/api/post/${post.id}/reject`);
            console.log(response.data); // 打印响应数据
            commit('REJECT_POST', {index, post: response.data}); // 提交mutation更新state
        } catch (error) {
            console.error(error);
        }
    },
    fetchUsers({commit}) {
        // 异步获取用户数据
        api.getUsers().then(users => {
            commit('SET_USERS', users);
        });
    },
    fetchPosts({commit}) {
        // 异步获取用户数据
        api.getUsers().then(posts => {
            commit('SET_POSTS', posts);
        });
    }
}